package com.oil.business.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.oil.business.domain.TActivityConfig;
import com.oil.business.domain.TActivityInfo;
import com.oil.business.mapper.TActivityConfigMapper;
import com.oil.business.mapper.TActivityInfoMapper;
import com.oil.business.service.ITActivityInfoService;
import com.oil.business.vo.ArrayData;
import com.oil.business.vo.TActivityInfoVo;
import com.oil.common.core.domain.model.LoginUser;
import com.oil.common.exception.ServiceException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 活动Service业务层处理
 *
 * @author ruoyi
 * @date 2023-01-05
 */
@Service
public class TActivityInfoServiceImpl implements ITActivityInfoService {
    @Autowired
    private TActivityInfoMapper tActivityInfoMapper;
    @Resource
    private TActivityConfigMapper tActivityConfigMapper;

    /**
     * 查询活动
     *
     * @param id 活动主键
     * @return 活动
     */
    @Override
    public TActivityInfoVo selectTActivityInfoById(Long id) {
        TActivityInfo tActivityInfo = tActivityInfoMapper.selectTActivityInfoById(id);
        TActivityInfoVo vo = new TActivityInfoVo();
        BeanUtils.copyProperties(tActivityInfo, vo);
        TActivityConfig config = new TActivityConfig();
        config.setActId(id);
        List<TActivityConfig> tActivityConfigs = tActivityConfigMapper.selectTActivityConfigList(config);
        List<ArrayData> arrayData = new ArrayList<>();
        for (TActivityConfig c : tActivityConfigs) {
            arrayData.add(new ArrayData(c.getTargetAmt().intValue(), c.getReduceAmt().intValue()));
        }
        vo.setArrayData(arrayData);
        return vo;
    }

    /**
     * 查询活动列表
     *
     * @param tActivityInfo 活动
     * @return 活动
     */
    @Override
    public List<TActivityInfo> selectTActivityInfoList(TActivityInfo tActivityInfo) {
        return tActivityInfoMapper.selectTActivityInfoList(tActivityInfo);

    }

    /**
     * 查询活动列表
     *
     * @param tActivityInfo 活动
     * @return 活动
     */
    @Override
    public List<TActivityInfoVo> selectTActivityInfoList1(TActivityInfo tActivityInfo) {
        List<TActivityInfo> tActivityInfos = tActivityInfoMapper.selectTActivityInfoList(tActivityInfo);
        List<TActivityInfoVo> res = new ArrayList<>();
        for (TActivityInfo i : tActivityInfos) {
            TActivityInfoVo vo = new TActivityInfoVo();
            BeanUtils.copyProperties(i, vo);
            TActivityConfig config = new TActivityConfig();
            config.setActId(i.getId());
            List<TActivityConfig> tActivityConfigs = tActivityConfigMapper.selectTActivityConfigList(config);
            List<ArrayData> arrayData = new ArrayList<>();
            for (TActivityConfig c : tActivityConfigs) {
                arrayData.add(new ArrayData(c.getTargetAmt().intValue(), c.getReduceAmt().intValue()));
            }
            vo.setArrayData(arrayData);
            res.add(vo);
        }
        return res;
    }

    /**
     * 新增活动
     *
     * @param tActivityInfo 活动
     * @param loginUser
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertTActivityInfo(TActivityInfoVo tActivityInfo, LoginUser loginUser) {
        int id = tActivityInfoMapper.insertTActivityInfo(tActivityInfo);
//        tActivityConfigMapper.delete(Wrappers.<TActivityConfig>lambdaQuery().eq(TActivityConfig::getActId, tActivityInfo.getId()));
        for (ArrayData data : tActivityInfo.getArrayData()) {
            TActivityConfig config = new TActivityConfig();
            config.setActId(tActivityInfo.getId());
            config.setCrtBy(loginUser.getUsername());
            config.setCrtTime(new Date());
            config.setReduceAmt(Long.parseLong(data.getReduceAmt()) * 100);
            config.setTargetAmt(Long.parseLong(data.getTargetAmt()) * 100);
            tActivityConfigMapper.insertTActivityConfig(config);
        }
        return tActivityInfoMapper.insertTActivityInfo(tActivityInfo);
    }

    /**
     * 修改活动
     *
     * @param tActivityInfo 活动
     * @return 结果
     */
    @Override
    public int updateTActivityInfo(TActivityInfo tActivityInfo) {
        return tActivityInfoMapper.updateTActivityInfo(tActivityInfo);
    }

    /**
     * 批量删除活动
     *
     * @param ids 需要删除的活动主键
     * @return 结果
     */
    @Override
    public int deleteTActivityInfoByIds(Long[] ids) {
        return tActivityInfoMapper.deleteTActivityInfoByIds(ids);
    }

    /**
     * 删除活动信息
     *
     * @param id 活动主键
     * @return 结果
     */
    @Override
    public int deleteTActivityInfoById(Long id) {
        return tActivityInfoMapper.deleteTActivityInfoById(id);
    }

    /**
     * 提交审批
     *
     * @param id
     * @return 结果
     */
    @Override
    public int publish(Long id) {
        TActivityInfoVo tActivityInfoVo = selectTActivityInfoById(id);

        if (tActivityInfoVo.getAppSt() == 10 || tActivityInfoVo.getAppSt() == 20) {
            throw new ServiceException("已经在审批中了，请耐心等待审批结果");
        }


        int publishSt = tActivityInfoVo.getPublishSt();
//        tActivityInfoVo.setPublishSt(publishSt);
//        区分上架还是下架审批
        if (publishSt == 0) {
            tActivityInfoVo.setAppSt(10);
        } else {
            tActivityInfoVo.setAppSt(20);
        }
        int i = updateTActivityInfo(tActivityInfoVo);
        return i;
    }
}
